package com.eprize;

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.Timer;

public class BoardComponent extends JComponent {
	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;
	private Grid grid;
	private int boardCount;

	public static final int ROW = 40;
	public static final int COL = 40;
	public static final int SIDE = 14;

	public BoardComponent() {
		grid = new Grid(ROW, COL);
	}

	public int getBoardCount() {
		return boardCount;
	}

	public void paintComponent(Graphics g) {
		boardCount = 0;
		for (int i = 0; i < ROW; i++)
			for (int j = 0; j < COL; j++) {
				g.drawRect(i * SIDE, j * SIDE, SIDE, SIDE);
				if (grid.getCell(i, j)) {
					g.fillRect(i * SIDE, j * SIDE, SIDE, SIDE);
					boardCount++;
				}
			}
	}

	public void initialize(int x, int y) {
		int row = Math.round(x / SIDE);
		int column = Math.round(y / SIDE);
		if (!grid.getCell(row, column))
			grid.makeCellAlive(row, column);
		else
			grid.makeCellDead(row, column);

		repaint();
	}

	public void generate() {
		grid.evolve();
		repaint();
	}

}
